<?php
/**
 * 仓库管理
 * @author QiuXiancCheng
 * @date 2016-07-03
 */
defined('APPLICATION_PATH') or die;
class Base_WarehouseController extends Controller_NeedLogin {

	/**
	 * 初始化
	 * {@inheritDoc}
	 * @see Controller_Base::init()
	 */
	public function init() {

		parent::init();
		$this->model = $this->warehouseModel = new Base_Model_Warehouse();
		$this->department = new Base_Model_Department;
	}

/*	public function indexAction() {
$getData = $this->getRequest()->getParams();
$params = array();
$params['page'] = isset($getData['page']) ? $getData['page'] : 1;
$params['perPage'] = $this->page_size;
$params['type'] = !empty($getData['type']) ? $getData['type'] : '';
$params['is_oversea'] = isset($getData['is_oversea']) ? $getData['is_oversea'] : 404;
$params['dept_id'] = isset($getData['dept_id']) ? $getData['dept_id'] : '';
$params['search_type'] = isset($getData['search_type']) ? $getData['search_type'] : '';
$params['keyword'] = isset($getData['keyword']) ? $getData['keyword'] : '';
// p($params);die;
$depts = $this->model->getWareDept();
$data = $this->model->getWarehouseList($params);
$page_link = $this->page($data['count']);
$this->smarty->assign('page_link', $page_link);
$this->smarty->assign('depts', $depts);
$this->smarty->assign('params',$params);
$this->smarty->assign('data', $data['list']);
$this->smarty->display('base_warehouse.tpl');
}*/

	/**
	 * 仓库查询主列表
	 */
	public function indexAction() {

		$params = $this->_PAGELIMITER($page);
		$this->warehouseModel->filterWhere($where, $page);
		$params['where'] = $where;
		$depts = $this->department->getDepartment(0, 1);
		$this->smarty->assign('params', json_encode($this->_request->getParams()))->assign('depts', $depts);
		$this->smarty->assign('depthtml', $this->department->gertDepartmentHTML($depts));
		$this->smarty->assign('conf', Helper_Lang::$conf);
		$this->smarty->assign('get', $page);
		$this->smarty->assign('page', $this->page($this->warehouseModel->getCount($params), $page['action']));
		$this->smarty->assign('list', $this->warehouseModel->getWarehouse($params));
		$this->smarty->display('warehouse_index.php');
	}

	public function getQuote() {

		$get = $this->_request->getParams();
	}

	/**
	 * 增加仓库信息 我应该是在做完
	 * @param $_POST
	 * @return Json
	 */
/*	public function addwarehouseAction() {

$canUpdate = 0;
$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1003']];
if ($this->_request->isPost()) {
$data = $this->_request->getPost();

// 验证表单
if ($c = $this->warehouseModel->checkFormAdd($data)) {
$result['message'] = Helper_Lang::$conf['wareSelected'][$c];
$this->_helper->json($result);
}

// 看看有没有该仓库名称 如果相同 不通增加
if ($this->warehouseModel->checkRepeatWarehouseName($data['warehouse_name']) != 0) {
$result['message'] = Helper_Lang::$conf['Err1025'];
$this->_helper->json($result);
}

// 检测是否存在相同的仓库代码
if ($this->warehouseModel->checkRepeatWarehouseCode($data['warehouse_code']) != 0) {
$result['message'] = Helper_Lang::$conf['Err1024'];
$this->_helper->json($result);
}

$data['warehouse_code'] = strtoupper($data['warehouse_code']);

$examineData = [
'l_leader' => $data['l_leader'],
'b_leader' => $data['b_leader'],
'director' => $data['director'],
'manager' => $data['manager'],
];
if (false !== ($checked = $this->warehouseModel->checkInputed($data))) {
$result['message'] = $checked;
return $this->_helper->json($result);
}
if ($data['demand_examine'] == 2) {
// 看下是否已经写了需求审核
foreach ($examineData as $v) {
if (!isset($v['auditor']) || (empty($v['quota']) && empty($v['maxnum']))) {
continue;
}
$canUpdate++;
}
if ($canUpdate == 0) {
$result['message'] = Helper_lang::$conf['Err1004'];
return $this->_helper->json($result);
}
}

unset($data['manager'], $data['director'], $data['l_leader'], $data['b_leader']);

$data['modified'] = $data['add_time'] = date('Y-m-d H:i:s', time());
$data['add_user'] = $data['modify_user'] = $this->_session->user_name;
$data['stock_en_name'] = $data['warehouse_code'];
if ($lastAddId = $this->warehouseModel->addWarehouse($data)) {
$sql = 'INSERT INTO c_warehouse_ex(auditor, quota, maxnum, warehouse_code) VALUES';
if ($data['demand_examine'] == 2) {
foreach ($examineData as $v) {
if (!isset($v['auditor']) || (empty($v['quota']) && empty($v['maxnum']))) {
continue;
}
empty($v['quota']) && $v['quota'] = 0;
empty($v['maxnum']) && $v['maxnum'] = 0;
$canUpdate++;
$v['warehouse_code'] = '"' . $data['warehouse_code'] . '"';
$sql .= '(' . implode(', ', $v) . '),';
}
Model_Db::getInstance()->query(rtrim($sql, ','));
}
$result['status'] = 1;
$result['message'] = Helper_Lang::$conf['Suc1001'] . Helper_Lang::$conf['Wiat'];
}
$this->_helper->json($result);
}
$deptHtml = $this->department->gertDepartmentHTML($this->department->getDepartment(0, 1));
$this->smarty->assign('deptHtml', $deptHtml);
$this->smarty->display('add.php');
}*/

	/**
	 * 仓库修改页面
	 */
	public function editAction() {

		$id = $this->_request->getParam('id');
		$warehouse = $this->warehouseModel->getWarehouse(['where' => ' WHERE id = ' . $id]);
		$examine = $this->warehouseModel->fetchWarehouseByCode($warehouse[0]['warehouse_code']);
		if (count($examine) > 0) {
			foreach ($examine as $value) {
				$warehouse_ex[$value['auditor']] = $value;
			}
			$this->smarty->assign('warehouse_ex', $warehouse_ex);
		}
		$this->smarty->assign('depts', $this->department->gertDepartmentHTML($this->department->getDepartment(0, 1)));
		$this->smarty->assign('warehouse', json_encode($warehouse));
		$this->smarty->display('edit.php');
	}

	/**
	 * 对仓库进行修改
	 * @return Json
	 */
	public function doeditAction() {

		$get = $this->_request->getParams();
		$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1006']];
		// 验证表单
		if ($c = $this->warehouseModel->checkFormAdd($get)) {
			$result['message'] = Helper_Lang::$conf['wareSelected'][$c];
			$this->_helper->json($result);
		}
		if (false !== ($checked = $this->warehouseModel->checkInputed($get))) {
			$result['message'] = $checked;
			return $this->_helper->json($result);
		}
		$updated = 0;
		if ($this->warehouseModel->editWarehouseExamineList($get)) {
			++$updated;
		}
		unset($get['l_leader'], $get['b_leader'], $get['director'], $get['manager']);
		// 更新主表
		if ($this->warehouseModel->editWarehouse($get)) {
			++$updated;
		}
		if ($updated) {
			$result['message'] = Helper_Lang::$conf['Suc1002'];
			$result['status'] = 1;
		}
		return $this->_helper->json($result);
	}

	public function getselectedwarehousebydeptidAction() {

		$did = $this->_request->getParam('did');
		$al = new Application_Model_Warehouse;
		exit(json_encode($al->getWarehouseByIds($did)));
	}

	/**
	 * 列表页批量修改仓库相关的审核信息
	 */
	public function changestatusAction() {

		$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1006']];
		$get = $this->_request->getParams();
		if ($get['demand_examine'] == '-') {
			unset($get['demand_examine']);
		}
		$warehouse_code = $get['warehouse_code'];
		unset($get['module'], $get['controller'], $get['action'], $get['warehouse_code']);
		// 批量修改审核 第一步3
		if ($this->warehouseModel->editWarehouseForList($get, $warehouse_code)) {
			$result['message'] = Helper_Lang::$conf['Suc1002'];
			$result['status'] = 1;
		}
		exit($this->_helper->json($result));
	}

	/**
	 * @return 批量重新对仓库审核表进行修改
	 */
	public function changewarehouseexamineAction() {

		$data = $this->_request->getParams();
		Helper_Array::filterModels($data);
		$this->warehouseModel->frilterLeader($data);
		$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1007']];
		if (count($data) <= 1) {
			return $this->_helper->json($result);
		}

		if (false !== ($checked = $this->warehouseModel->checkInputed($data))) {
			$result['message'] = $checked;
			return $this->_helper->json($result);
		}
		// 删除审核表的旧东西
		$this->warehouseModel->deleteWarehouse_ex(['warehouse_code' => $data['warehouse_code']]);
		if ($this->warehouseModel->editWarehouseExamineList_Index($data)) {
			$result['message'] = Helper_Lang::$conf['Suc1002'];
			$result['status'] = 1;
		}
		return $this->_helper->json($result);
	}

	/**
	 * ebay海外仓列表
	 * @return [type] [description]
	 */
	public function ebayListAction() {
		$this->smarty->display('ebayWarehouseList.tpl');
	}
}